﻿using Engine;
using Game;
using System.Collections.Generic;

namespace Game
{
    public class BlockColorProcessor
    {
        private readonly Dictionary<int, BlockPixelData> _blockPixelDatas;
        private readonly SubsystemTerrain _subsystemTerrain;

        private readonly Dictionary<int, Color> _customBlockColors = new Dictionary<int, Color>
        {
            {2, new Color(139, 69, 19, 255)},    // 泥土
            {7, new Color(210, 180, 140, 255)},  // 沙子
            {8, new Color(34, 139, 34, 255)},    // 草地
            {18, new Color(0, 100, 200, 230)},   // 水
            {12, new Color(34, 139, 34, 255)},   // 橡叶
            {13, new Color(107, 142, 35, 255)},  // 白桦叶
            {14, new Color(22, 84, 22, 255)},    // 云杉叶
            {15, new Color(240, 248, 255, 180)}, // 玻璃
            {20, new Color(255, 0, 0, 255)},     // 红花
            {24, new Color(128, 0, 128, 255)}    // 紫花
        };

        public BlockColorProcessor(Dictionary<int, BlockPixelData> blockPixelDatas, SubsystemTerrain subsystemTerrain)
        {
            _blockPixelDatas = blockPixelDatas;
            _subsystemTerrain = subsystemTerrain;
        }

        public Color GetColor(int content, Vector3 playerPosition, int x, int z)
        {
            int blockId = content & 255;
            Color baseColor = GetBaseColor(blockId);
            return ApplyEnvironmentEffects(baseColor, blockId, x, z);
        }

        private Color GetBaseColor(int blockId)
        {
            if (_customBlockColors.TryGetValue(blockId, out Color customColor))
            {
                return customColor;
            }
            return _blockPixelDatas.TryGetValue(blockId, out var data) ? data.Color : Color.Black;
        }

        private Color ApplyEnvironmentEffects(Color baseColor, int blockId, int x, int z)
        {
            if (!_blockPixelDatas.TryGetValue(blockId, out var data) || !data.NeedChangeWithEnvironment)
            {
                return baseColor;
            }

            int topHeight = _subsystemTerrain.Terrain.GetTopHeight(x, z);
            int temperature = _subsystemTerrain.Terrain.GetSeasonalTemperature(x, z)
                            + SubsystemWeather.GetTemperatureAdjustmentAtHeight(topHeight);
            int humidity = _subsystemTerrain.Terrain.GetSeasonalHumidity(x, z);

            return blockId switch
            {
                8 => BlockColorsMap.Grass.Lookup(temperature, humidity) * baseColor,
                12 => BlockColorsMap.OakLeaves.Lookup(temperature, humidity) * baseColor,
                13 => BlockColorsMap.BirchLeaves.Lookup(temperature, humidity) * baseColor,
                14 => BlockColorsMap.SpruceLeaves.Lookup(temperature, humidity) * baseColor,
                18 => BlockColorsMap.Water.Lookup(temperature, humidity) * baseColor,
                225 => BlockColorsMap.TallSpruceLeaves.Lookup(temperature, humidity) * baseColor,
                256 => BlockColorsMap.MimosaLeaves.Lookup(temperature, humidity) * baseColor,
                _ => baseColor
            };
        }
    }
}